require 'gadgets/combobox'
module EventEditor
  module Commands
    class CallEventPage
      def self.show(base, model, edit_command = [], options = {}, &func)
       @dialog = Gtk::Dialog.new("Call Event Page",
               base,
               Gtk::Dialog::MODAL,
               ["OK", Gtk::Dialog::RESPONSE_OK],
               ["Cancel", Gtk::Dialog::RESPONSE_CANCEL]
               )

        @model = model
        @dialog.vbox.add(self.create_panel)
        @dialog.vbox.show_all

        self.load(edit_command)
        
        @dialog.run do |response|
          if response == Gtk::Dialog::RESPONSE_OK
            func.call(self.command)
          end
        end
        @dialog.destroy
      end
      
      def self.create_panel
        ## Create document
        box = Gtk::VBox.new
        
        # Speaker Selection
        @event_combobox = ::Gadgets::Combobox.new(@model.event_select_list(:events), [String, Integer])
        @event_combobox.signal_connect('changed') do
          if @event_combobox.selected_data == -1
            event = @model.selected_event
          else
            event = @model.get_event_by_id(@event_combobox.selected_data)
          end
          @event_page_box.set_range(1, event['pages'].length)
        end
        
        @event_page_box = Gtk::SpinButton.new(1, 3, 1)
        
        box.add(@event_combobox)
        box.add(@event_page_box)
        return box
      end
      
      def self.load(edit_command)
        if edit_command.empty?
          
        else
          @event_combobox.select(edit_command[1])
          @event_page_box.value = edit_command[2] + 1
        end
      end
      
      def self.command
        return ['call_event_page', @event_combobox.selected_data, @event_page_box.value.floor - 1]
      end
      
      def self.command_to_s(model, args, margin = "")
        
        name = '[<span color="red">invalid</span>]'
        model.event_select_list.each do |t|
          if t[1] == args[1]
            name = "[ " + t[0] + " ]"
            break
          end
        end
        
        s = "<b>Call Event Page:</b> #{name} #{args[2] + 1} page"

        return s
      end
      
      def self.icon
        return @@icon
      end
    end
  end
end